package com.yupi.springbootinit.scoring;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yupi.springbootinit.model.dto.question.QuestionContentDTO;
import com.yupi.springbootinit.model.entity.App;
import com.yupi.springbootinit.model.entity.Question;
import com.yupi.springbootinit.model.entity.ScoringResult;
import com.yupi.springbootinit.model.entity.UserAnswer;
import com.yupi.springbootinit.service.QuestionService;
import com.yupi.springbootinit.service.ScoringResultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy {
    @Resource
    private QuestionService questionService;
    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScore(List<String> choices, App app) {
        Question question = questionService.getOne(new LambdaQueryWrapper<Question>()
                .eq(Question::getAppId, app.getId()));
        List<ScoringResult> scoringResultList = scoringResultService.list(new LambdaQueryWrapper<ScoringResult>()
                .eq(ScoringResult::getAppId, app.getId()).orderByDesc(ScoringResult::getResultScoreRange));
        String questionContent = question.getQuestionContent();
        List<QuestionContentDTO> questionContentDTOList = JSONUtil.toList(questionContent, QuestionContentDTO.class);

//        HashMap<String, Integer> optionCount = new HashMap<>();
        int totalScore=0;
        for (QuestionContentDTO contentDTO : questionContentDTOList) {
            for (String choice : choices) {
                for (QuestionContentDTO.Option option : contentDTO.getOptions()) {
                    if (option.getKey().equals(choice)) {
//                        optionCount.put(choice, optionCount.getOrDefault(choice, 0) + 1);
                        totalScore+= Optional.of(option.getScore()).orElse(0);
                    }
                }
            }
        }
        int maxScore = 0;
        ScoringResult scoringResult = scoringResultList.get(0);
        for (ScoringResult result : scoringResultList) {
            if(result.getResultScoreRange()<=totalScore){
                maxScore=totalScore;
                scoringResult=result;
                break;
            }
        }
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setAppId(app.getId());
        userAnswer.setResultDesc(scoringResult.getResultDesc());
        userAnswer.setResultName(scoringResult.getResultName());
        userAnswer.setResultPicture(scoringResult.getResultPicture());
        userAnswer.setResultId(scoringResult.getId());
        userAnswer.setResultScore(maxScore);

        return userAnswer;
    }
}

